import pygame
from random import randint

pygame.init()

WIN_WIDTH = 400
WIN_HEIGHT = 600

window = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption('鼠标事件')
window.fill((255, 255, 255))

pygame.display.flip()

while True:
    # 检测事件
    for event in pygame.event.get():

        """
            MOUSEBUTTONDOWN     -- 按下
            MOUSEBUTTONUP       -- 弹起
            event.pos           -- 取出鼠标当前坐标
        """

        if event.type == pygame.QUIT:
            exit()

        # 鼠标按下
        if event.type == pygame.MOUSEBUTTONDOWN:
            # pygame.draw.rect(window, (0, 0, 0), (100, 100, 200, 200))
            # pygame.display.update()
            print('鼠标按下')
        # 鼠标弹起
        if event.type == pygame.MOUSEBUTTONUP:
            mx, my = event.pos  # 取出鼠标当前坐标
            pygame.draw.circle(window, (255, 255, 0), (mx, my), 30)
            pygame.display.update()
        if event.type == pygame.MOUSEMOTION:
            mx, my = event.pos
            r = randint(0, 255)
            g = randint(0, 255)
            b = randint(0, 255)
            pygame.draw.rect(window, (r, g, b), (mx, my, 50, 50), 1)
            pygame.display.update()
